/********************************************************
 * Filename: TclShell.java
 * Author: LMU
 * Program: TUnit
 * Description: Creates the tcl shell for test execution.   
 * Revision History:
 * 20060904: Initial Revision.  Added header later.  
 * *****************************************************/

package edu.lmu.cs.tUnit.popup.shell;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import edu.lmu.cs.tUnit.log.TUnitLog;

/**
 * Created StreamGobbler to output to the console and 
 * read the results
 * @author LMU
 *
 */
class StreamGobbler extends Thread {
	InputStream is;
	MessageConsoleStream stream;
	private int numFailures = 0;
	private int noFile = 0;
    
	StreamGobbler(InputStream is, MessageConsoleStream stream) {
		this.is = is;
		this.stream = stream;
	}

	public void run() {
		try {
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			
			// Look for failures
			while ( (line = br.readLine()) != null) {
				stream.println(line);
				if(line.indexOf("*FAILED*") != -1)
					numFailures++;
				if(line.indexOf("can't read") != -1)
					numFailures++;
				if(line.indexOf("no such file or directory") != -1)
					noFile++;
			}
		} catch (IOException ioe) {
			TUnitLog.logError("Reader failure.", ioe);
		}
    }
	
	public int getFailures() {
		return numFailures;
	}
	
	public int getNoFile() {
		return noFile;
	}
}

public class TclShell {
	private int numFailures;
	
	/**
	 * Start the tcl shell within the console and execute the script
	 * @param fileName
	 * @param console
	 * @param path
	 */
	public TclShell(String fileName, MessageConsole console, String path) {
		try {
			console.activate();
			MessageConsoleStream stream = console.newMessageStream();
			ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{console});
			String cmd = "tclsh ";
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(cmd + fileName, null, new File(path));
		
			// Output error message
			StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), stream);
			
			// Output from run
			StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), stream);
			
			// start readers
            errorGobbler.start();
            outputGobbler.start();
            proc.waitFor();
            numFailures = errorGobbler.getFailures() + outputGobbler.getFailures();
            if((errorGobbler.getNoFile() + outputGobbler.getNoFile()) >= 1)
            	numFailures = -1;
			
		} catch (Throwable t) {
			TUnitLog.logError("TclShell failure.", t);
		}
	}	
	
	public int getFailures() {
		return numFailures;
	}
}
